package com.renjibo.sharkerwave.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.renjibo.sharkerwave.R;
import com.renjibo.sharkerwave.models.Course;

import org.xutils.x;

import java.util.List;

/**
 * 第一种布局的适配 免费试听
 * 任继波
 * Created by Administrator on 2017/4/10.
 */

public class HomeOneAdapter extends RecyclerView.Adapter<HomeOneAdapter.OneViewHolder> {

    private final LayoutInflater inflater;
    private List<Course> tryCourse;
    private Context context;
    private RecyclerViewItemOnClick recyclerViewItemOnClick;
    public HomeOneAdapter(Context context, List<Course> tryCourse){
        this.context=context;
        this.inflater = LayoutInflater.from(context);
        this.tryCourse=tryCourse;
    }
    public void setRecyclerViewItemOnClick(RecyclerViewItemOnClick recyclerViewItemOnClick){
        this.recyclerViewItemOnClick=recyclerViewItemOnClick;
    }
    @Override
    public OneViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=inflater.inflate(R.layout.home_one_recycler_item,parent,false);
        return new OneViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OneViewHolder holder, final int position) {
//        Log.d("HomeOneAdapter","免费试听"+tryCourse.size());
        final Course course = tryCourse.get(position);
        holder.home_one_Recycler_text_title.setText(course.title+"");
        holder.home_one_Recrcler_text_msg.setText(course.title2+"");
        holder.home_one_recycler_text_name.setText(course.speaker+"");
        holder.home_one_Recycler_text_length.setText(course.length+"");
        if(course.type==1){
            //小图片
            Glide.with(context).load(R.drawable.home_image_small_video).into(holder.home_one_Recycler_image_small);
        }else{
            Glide
                    .with(context)
                    .load(R.drawable.home_image_small_music)
                    .centerCrop()
                    .placeholder(R.drawable.home_image_small_music)
                    .crossFade()
                    .into(holder.home_one_Recycler_image_small);
        }
        //加载图片
        Glide.with(context).load(course.image)
                .centerCrop()
                .placeholder(R.drawable.content_picture)
                .crossFade()
                .error(R.drawable.content_picture)
                .into(holder.home_one_recycler_imageView);
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(recyclerViewItemOnClick!=null)
                    recyclerViewItemOnClick.onItemClick(position,course.object_id);
            }
        });
    }

    @Override
    public int getItemCount() {
        return tryCourse.size();
    }

    class OneViewHolder extends RecyclerView.ViewHolder{
        private final ImageView home_one_recycler_imageView;
        private final ImageView home_one_Recycler_image_small;
        private final TextView home_one_Recycler_text_title;
        private final TextView home_one_Recrcler_text_msg;
        private final TextView home_one_recycler_text_name;
        private final TextView home_one_Recycler_text_length;
        private final View view;

        public OneViewHolder(View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.home_one_recycler_auto_linear);
            home_one_recycler_imageView = (ImageView) itemView.findViewById(R.id.home_one_recycler_imageView);
            home_one_Recycler_image_small = (ImageView) itemView.findViewById(R.id.home_one_Recycler_image_small);
            home_one_Recycler_text_title = (TextView) itemView.findViewById(R.id.home_one_Recycler_text_title);
            home_one_Recrcler_text_msg = (TextView) itemView.findViewById(R.id.home_one_Recrcler_text_msg);
            home_one_recycler_text_name = (TextView) itemView.findViewById(R.id.home_one_recycler_text_name);
            home_one_Recycler_text_length = (TextView) itemView.findViewById(R.id.home_one_Recycler_text_length);
        }
    }
}
